package br.com.fox.iebav.session;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import javax.annotation.PostConstruct;
import javax.ejb.EJB;
import javax.enterprise.context.SessionScoped;
import javax.faces.application.FacesMessage;
import javax.faces.model.SelectItem;
import javax.inject.Named;

import br.com.fox.iebav.controller.command.SalvarFamilia;
import br.com.fox.iebav.controller.command.SalvarSubFamilia;
import br.com.fox.iebav.dao.FamiliaDAO;
import br.com.fox.iebav.entity.Familias;
import br.com.fox.iebav.entity.Niveis;
import br.com.fox.iebav.entity.Subfamilias;
import br.com.fox.iebav.session.abstractsession.AbstractSession;

@Named("subFamiliaBeanSession")
@SessionScoped
public class SubFamiliaBeanSession extends AbstractSession implements Serializable {
	
	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;
	
//	ATRIBUTOS
	private List<Subfamilias> subFamiliasList;
	private Subfamilias subFamiliaSelecionado;
	private List<SelectItem> familiasList;

//	EJBs
	private @EJB FamiliaDAO familiaDAO;
	
//	CONTROLLER COMMAND
	private @EJB SalvarSubFamilia salvarSubFamilia;
	private Map<String, Object> resultMap;
	
	
	@PostConstruct
	public void init()
	{
		setSubFamiliaSelecionado(new Subfamilias());
		carregarSubFamilias();
		redirecionarURL("subFamilias.java");
	}		
	
	public void carregarSubFamilias()
	{
		setSubFamiliasList( salvarSubFamilia.getSubfamiliasdao().findAll() );
	}
	
	public void carregarFamilias()
	{
		setFamiliasList(new ArrayList<SelectItem>());
		List<Familias> list = familiaDAO.findAll();
		for(Familias n : list)
			getFamiliasList().add( new SelectItem(n,n.getDescricao()) );
	}
	
	public void abrirSubFamiliaSelecionada()
	{
		redirecionarURL("subFamiliaPersistencia.java");
	}
	
	public void novaSubFamilia()
	{
		setSubFamiliaSelecionado(new Subfamilias());
		carregarFamilias();
		redirecionarURL("subFamiliaPersistencia.java");
	}
	
	public void salvarSubFamilia()
	{
		resultMap = salvarSubFamilia.execute(getSubFamiliaSelecionado());
		if( ( (Boolean) resultMap.get("resultado") ) )
			facesMensagens(FacesMessage.SEVERITY_INFO,((String)resultMap.get("mensagem")),"");
		else
			facesMensagens(FacesMessage.SEVERITY_ERROR,((String)resultMap.get("mensagem")),"");
		carregarSubFamilias();
	}

	public List<Subfamilias> getSubFamiliasList() {
		return subFamiliasList;
	}

	public void setSubFamiliasList(List<Subfamilias> subFamiliasList) {
		this.subFamiliasList = subFamiliasList;
	}

	public Subfamilias getSubFamiliaSelecionado() {
		return subFamiliaSelecionado;
	}

	public void setSubFamiliaSelecionado(Subfamilias subFamiliaSelecionado) {
		this.subFamiliaSelecionado = subFamiliaSelecionado;
	}

	public List<SelectItem> getFamiliasList() {
		return familiasList;
	}

	public void setFamiliasList(List<SelectItem> familiasList) {
		this.familiasList = familiasList;
	}

}
